package com.pingan.abc.onenews.view;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.pingan.abc.onenews.R;
import com.pingan.abc.onenews.view.fragment.ChatFragment;
import com.pingan.abc.onenews.view.fragment.MainFragment;
import com.pingan.abc.onenews.view.fragment.MoreFragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainContainerActivity extends FragmentActivity {
    public static final String _FRAGMENT_MAIN = "main";
    public static final String _FRAGMENT_CHAT = "chat";
    public static final String _FRAGMENT_MORE = "more";
    BottomNavigationView navigationView;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    private String currentSelectedTag = _FRAGMENT_MAIN;

    private Fragment fragmentMain, fragmentChar, fragmentMore;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_container);
        Logger.addLogAdapter(new AndroidLogAdapter());
        initView();
    }

    private void initView() {
        fragmentManager = getSupportFragmentManager();
        navigationView = findViewById(R.id.nav_view);
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        navigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
        fragmentMain = MainFragment.newInstance();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.nav_host_fragment, fragmentMain, _FRAGMENT_MAIN);
        fragmentTransaction.commit();
        currentFragment = fragmentMain;
    }

    BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.navigation_home:
                    if (currentSelectedTag.equals(_FRAGMENT_MAIN))
                        return true;

                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.hide(currentFragment);
                    fragmentTransaction.show(fragmentMain);
//                    fragmentTransaction.replace(R.id.nav_host_fragment,fragmentMain,_FRAGMENT_MAIN);
                    fragmentTransaction.commit();
                    currentFragment = fragmentMain;
                    currentSelectedTag = _FRAGMENT_MAIN;

                    break;
                case R.id.navigation_dashboard:
                    if (currentSelectedTag.equals(_FRAGMENT_CHAT))
                        return true;
                    if (null == fragmentChar) {
                        fragmentChar = ChatFragment.newInstance();
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.hide(currentFragment);
                        fragmentTransaction.add(R.id.nav_host_fragment, fragmentChar);
                        fragmentTransaction.commit();
                    } else {
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.hide(currentFragment);
//                    fragmentTransaction.replace(R.id.nav_host_fragment,fragmentChar,_FRAGMENT_CHAT);
                        fragmentTransaction.show(fragmentChar);
                        fragmentTransaction.commit();
                    }

                    currentFragment = fragmentChar;
                    currentSelectedTag = _FRAGMENT_CHAT;

                    break;
                case R.id.navigation_more:
                    if (currentSelectedTag.equals(_FRAGMENT_MORE))
                        return true;
                    if (null == fragmentMore) {
                        fragmentMore = new MoreFragment();
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.hide(currentFragment);
                        fragmentTransaction.add(R.id.nav_host_fragment, fragmentMore);
                        fragmentTransaction.commit();
                    } else {
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.hide(currentFragment);
//                    fragmentTransaction.replace(R.id.nav_host_fragment,fragmentMore,_FRAGMENT_MORE);
                        fragmentTransaction.show(fragmentMore);
                        fragmentTransaction.commit();
                    }
                    currentFragment = fragmentMore;
                    currentSelectedTag = _FRAGMENT_MORE;
                    break;
            }
            return true;
        }
    };

}
